package leetcode.editor.vscdev.hot100;

import java.util.ArrayDeque;

public class lc20_isValid {
    
}
class Solution {
    public boolean isValid(String s) {
        ArrayDeque<Character> stack = new ArrayDeque<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (c == '[' || c == '{' || c == '(') {
                stack.push(c);   
            } else {
                if (stack.isEmpty()) {
                    // 栈为空，说明右括号没有对应的左括号，返回false
                    return false;
                }
                char cc = stack.pop();
                if (!(c == ']' && cc == '[') && !(c == '}' && cc == '{') && !(c == ')' && cc == '(')) {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}